﻿using System;
using System.Runtime.InteropServices;

namespace Ultima.Xna
{
    /// <summary>
    /// A static class that stores a .Net PInvoke function calls to Win32 or any other Native Dll
    /// </summary>
    public static class NativeMethods
    {
        public const uint ATTACH_PARENT_PROCESS = 0x0FFFFFFFF;

        /// <summary>
        /// The _lread function reads data from the specified file. This function is provided for compatibility with 16-bit versions of Windows
        /// </summary>
        /// <param name="hFile">handle to file</param>
        /// <param name="lpBuffer">pointer to buffer for read data</param>
        /// <param name="wBytes">length, in bytes, of data buffer</param>
        /// <returns>The return value indicates the number of bytes actually read from the file. 
        /// If the number of bytes read is less than uBytes, the function has reached the end of 
        /// file (EOF) before reading the specified number of bytes.</returns>
        [DllImport("Kernel32")]
        public unsafe static extern int _lread(IntPtr hFile, void* lpBuffer, int wBytes);

        /// <summary>
        /// Allocates a new console for the calling process.
        /// </summary>
        /// <returns>If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero. 
        /// To get extended error information, call GetLastError.</returns>
        [DllImport("kernel32", SetLastError = true)]
        public static extern bool AllocConsole();

        /// <summary>
        /// Detaches the calling process from its console.
        /// </summary>
        /// <returns>If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero.
        /// To get extended error information, call GetLastError.</returns>
        [DllImport("kernel32", SetLastError = true)]
        public static extern bool FreeConsole();

        /// <summary>
        /// Attaches the calling process to the console of the specified process.
        /// </summary>
        /// <param name="dwProcessId"><para>The identifier of the process whose console is to be used. This parameter can be one of the following values.
        /// <para>Use the console of the specified process.</para>
        /// <para>ATTACH_PARENT_PROCESS = Use the console of the parent of the current process.</para></para>
        /// </param>
        /// <returns>If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero.
        /// To get extended error information, call GetLastError.</returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool AttachConsole(uint dwProcessId);
    }
}
